﻿using Newtonsoft.Json;

namespace Mohe.Scada.Util.Models
{
    public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
    public class ValueChangedEventArgs : EventArgs
    {
        public ValueChangedEventArgs(object value)
        {
            this.Value = value;
        }

        public object Value;
    }
    public class Tag
    {
        public string VarName { get; set; } = string.Empty;
        public EnumDataType DataType { get; set; }
        /// <summary>
        /// 保护级别
        /// </summary>
        public EnumProtectType ProtectType { get; set; }

        private object _cookedValue;
        [JsonIgnore]
        public object CookedValue
        {
            get => _cookedValue;
            set
            {
                if (_cookedValue != value)
                {
                    _updateTime = DateTime.Now;
                    _cookedValue = value;
                    if (ValueChanged != null)
                    {
                        ValueChanged(this, new ValueChangedEventArgs(_cookedValue));
                    }
                }
            }
        }
        private DateTime _updateTime;
        [JsonIgnore]
        public DateTime UpdateTime { get => _updateTime; set => _updateTime = value; }
        [JsonIgnore]
        public ValueChangedEventHandler ValueChanged;

        public EnumVariableStatus StatusType { get; set; }

        #region 附加属性
        [JsonIgnore]
        public int Pos { get; set; }
        /// <summary>
        /// 比特位
        /// </summary>
        [JsonIgnore]
        public int Bit { get; set; }

        /// <summary>
        /// 报警
        /// </summary>
        [JsonIgnore]
        public bool IsWarning { get; set; }
        /// <summary>
        /// 报警类型
        /// </summary>
        [JsonIgnore]
        public short WarningType { get; set; }
        /// <summary>
        /// 报警内容
        /// </summary>
        [JsonIgnore]
        public string WarningContent { get; set; } = string.Empty;
        [JsonIgnore]
        public bool NeedNegate { get; set; }
        [JsonIgnore]
        public int? Sort { get; set; }
        #endregion

        #region 实体映射
        [JsonIgnore]
        public Type TypeName { get; set; }
        [JsonIgnore]
        public object Model { get; set; }
        [JsonIgnore]
        public string FieldName { get; set; } = string.Empty;
        #endregion
    }

    public class TagComparer : IComparer<Tag>
    {
        public int Compare(Tag x, Tag y)
        {
            return x.VarName.CompareTo(y.VarName);
        }
    }
}
